package com.tang.controller;

import com.github.pagehelper.PageInfo;
import com.tang.dto.BookDTO;
import com.tang.resp.JsonResp;
import com.tang.service.impl.BookServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/book/*")
public class BookController {
    @Autowired
    private BookServiceImpl bookService;

    @GetMapping("all")
    public JsonResp all(){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        resp.setMessage("查询书籍成功");
        List<BookDTO> all = bookService.all();
        resp.setData(all);
        return resp;
    }
    @Validated
    @GetMapping("list")
//    @NotNull：用在基本类型的包装类型上面的属性注解，不能为null，但可以为empty
//
//    @NotEmpty：用在集合类上面的属性的注解，不能为null，而且长度必须大于0
//
//    @NotBlank：用在String上面属性的注解，不能为null，而且调用trim()后，长度必须大于0
    public JsonResp list(String category2Id, String name, @NotNull int pageNum, @NotNull int pageSize){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        resp.setMessage("查询书籍成功");
        Map<String, Object> map = bookService.list(category2Id, name, pageNum, pageSize);
        resp.setData(map);
        return resp;
    }
    @PostMapping("update")
    public JsonResp update(@RequestBody BookDTO bookDTO){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        int update = bookService.update(bookDTO);
        if (update>0){
            resp.setMessage("更新书籍成功");
        }else {
            resp.setMessage("更新书籍失败");
        }
        resp.setData(update);
        return resp;
    }
    @DeleteMapping("delete/{id}")
    public JsonResp delete(@PathVariable("id") String id){
        JsonResp resp = new JsonResp();
        resp.setCode(200);
        int delete = bookService.delete(id);
        if (delete>0){
            resp.setMessage("删除书籍成功");
        }else {
            resp.setMessage("删除书籍失败");
        }
        resp.setData(delete);
        return resp;
    }
}
